package com.dinadi.yidi.ui.activity;

import android.content.Intent;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.dinadi.yidi.R;
import com.dinadi.yidi.adapter.CreativeProductAdapter;
import com.dinadi.yidi.base.BaseActivity;
import com.dinadi.yidi.bean.CreativeProductBean;
import com.dinadi.yidi.bean.CreativeProductRequest;
import com.dinadi.yidi.bean.CreativeProductRequestEntity;
import com.dinadi.yidi.http.HttpListener;
import com.dinadi.yidi.http.interfaces.CommonInterface;
import com.dinadi.yidi.utils.JsonUtil;
import com.dinadi.yidi.utils.SharePreferencesHelper;
import com.dinadi.yidi.utils.pullloadmorerecyclerview.PullLoadMoreRecyclerView;

import java.util.ArrayList;
import java.util.List;

/**
 * author: JiaYu.He
 * Date: 2017/3/24   首页-创意产品
 * Time: 11:02
 */

public class CreativeProductActivity extends BaseActivity {
    private View view;
    private TextView tvTitle;
    private PullLoadMoreRecyclerView mPullLoadMoreRecyclerView;
    private CreativeProductAdapter mRefreshAdapter;
    private List<CreativeProductRequestEntity> dataList = new ArrayList<>();
    private int page = 1;
    private String subjectProductId, token;

    @Override
    protected View getLayout() {
        view = LayoutInflater.from(CreativeProductActivity.this).inflate(R.layout.activity_message, null);
        return view;
    }

    @Override
    protected void noWifiRefresh() {

    }

    @Override
    protected void initView() {
        changePager(2);
        tvTitle = (TextView) view.findViewById(R.id.text_title);
        tvTitle.setText(getResources().getString(R.string.text_creative_product));
        mPullLoadMoreRecyclerView = (PullLoadMoreRecyclerView) view.findViewById(R.id.pullLoadMoreRecyclerView);
        mPullLoadMoreRecyclerView.setGridLayout(2);
        mRefreshAdapter = new CreativeProductAdapter(CreativeProductActivity.this);
        mPullLoadMoreRecyclerView.setAdapter(mRefreshAdapter);
        mPullLoadMoreRecyclerView.setOnPullLoadMoreListener(new PullLoadMoreListener());
    }

    @Override
    protected void initListener() {


    }

    @Override
    protected void initData() {

        Intent intent = getIntent();
        subjectProductId = intent.getStringExtra("subjectProductId");
        token = SharePreferencesHelper.getInstance(this).getString("token");
        showLoadingDialog(getResources().getString(R.string.dialog_text));
        initPullrefresh();


    }


    @Override
    protected void clickEvent(View v) {

    }

    HttpListener httpListener = new HttpListener() {

        @Override
        public void OnSuccess(String response) {
            super.OnSuccess(response);
            String errCode = JsonUtil.getFieldValue(response, "errCode");
            String errMsg = JsonUtil.getFieldValue(response, "errMsg");
            if (errCode.equals("0000")) {
                closeLoadingDialog();
                CreativeProductRequest bean = JsonUtil.parseJsonToBean(response, CreativeProductRequest.class);
                dataList = bean.getProductList();
                mRefreshAdapter.addAllData(dataList);
                getData();


            } else {
                closeLoadingDialog();
                toast(errMsg);
            }


        }

        @Override
        public void OnFail(String errorResponse) {
            super.OnFail(errorResponse);
            closeLoadingDialog();
            toast("加载失败");
        }
    };

    private void getData() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                //刷新结束
                mPullLoadMoreRecyclerView.setPullLoadMoreCompleted();
            }
        }, 1000);
    }

    class PullLoadMoreListener implements PullLoadMoreRecyclerView.PullLoadMoreListener {
        @Override
        public void onRefresh() {
            setRefresh();
        }

        @Override
        public void onLoadMore() {
            page = page + 1;
            initPullrefresh();
        }
    }

    private void setRefresh() {
        mRefreshAdapter.clearData();
        page = 1;
        initPullrefresh();
    }

    private void initPullrefresh() {
        CreativeProductBean bean = new CreativeProductBean();
        bean.setToken(token);
        bean.setSubjectId(subjectProductId);
        bean.setPageNum(page + "");
        bean.setPageSize("10");
        CommonInterface creativeProductInterface = new CommonInterface(CreativeProductActivity.this, httpListener);
        creativeProductInterface.getInterface("14004", JSON.toJSONString(bean));
    }


}
